package com.jiyuren.wheelview.view;

import java.util.List;

import android.view.View;

import com.jiyuren.wheelview.R;
import com.jiyuren.wheelview.adapter.ArrayWheelAdapter;
import com.jiyuren.wheelview.lib.OptionType;
import com.jiyuren.wheelview.lib.WheelView;
import com.jiyuren.wheelview.listener.OnItemSelectedListener;

public class WheelOptions<T> {
  private View view;
  private WheelView wv_option1;
  private WheelView wv_option2;
  private WheelView wv_option3;

  private List<T> mOptions1Items;
  private List<List<T>> mOptions2Items;
  private List<List<List<T>>> mOptions3Items;

  private boolean linkage = false;
  private OnItemSelectedListener wheelListener_option1;
  private OnItemSelectedListener wheelListener_option2;
  
  public View getView() {
    return view;
  }

  public void setView(View view) {
    this.view = view;
  }

  public WheelOptions(View view) {
    super();
    this.view = view;
    setView(view);
  }
  
  
  public void setPicker(List<T> optLs1) {
    setPicker(optLs1, null, null, false);
  }

  public void setPicker(List<T> optLs1, List<List<T>> optLs2, boolean linkage) {
    setPicker(optLs1, optLs2, null, linkage);
  }

  public void setPicker(List<T> optLs1, List<T> optLs2){
    setPicker(optLs1,optLs2,null);
  }
  /**
   * 独立集合
   * @param options1Items
   * @param options2Items
   * @param options3Items
   */
  public void setPicker(List<T> optLs1, List<T> optLs2, List<T> optLs3){
    int len = ArrayWheelAdapter.DEFAULT_LENGTH;
    int textSize = 20;
    if (optLs2 == null){
      len = 12;
    }else{
      textSize = 20; 
    }
    if (optLs3 == null){
      len = 8;
    }else{
      textSize = 18;
    }
    // 选项1
    wv_option1 = (WheelView) view.findViewById(R.id.options1);
    wv_option1.setAdapter(new ArrayWheelAdapter(optLs1, len));// 设置显示数据
    wv_option1.setCurrentItem(0);// 初始化时显示的数据
    // 选项2
    wv_option2 = (WheelView) view.findViewById(R.id.options2);
    if (optLs2 != null)
      wv_option2.setAdapter(new ArrayWheelAdapter(optLs2));// 设置显示数据
    wv_option2.setCurrentItem(0);// 初始化时显示的数据
    // 选项3
    wv_option3 = (WheelView) view.findViewById(R.id.options3);
    if (optLs3 != null)
      wv_option3.setAdapter(new ArrayWheelAdapter(optLs3));// 设置显示数据
    wv_option3.setCurrentItem(0);// 初始化时显示的数据


    wv_option1.setTextSize(textSize);
    wv_option2.setTextSize(textSize);
    wv_option3.setTextSize(textSize);

    if (optLs2 == null)
      wv_option2.setVisibility(View.GONE);
    if (optLs3 == null)
      wv_option3.setVisibility(View.GONE);
  }
  
  
  @SuppressWarnings({ "unchecked", "rawtypes" })
  public void setPicker(List<T> options1Items, List<List<T>> options2Items, List<List<List<T>>> options3Items,
      boolean linkage) {
    this.linkage = linkage;
    this.mOptions1Items = options1Items;
    this.mOptions2Items = options2Items;
    this.mOptions3Items = options3Items;
    int len = ArrayWheelAdapter.DEFAULT_LENGTH;
    int textSize = 20;
    if (this.mOptions2Items == null){
      len = 12;
    }else{
      textSize = 18;
    }
    if (this.mOptions3Items == null){
      len = 8;
    }else{
      textSize = 16;
    }
    // 选项1
    wv_option1 = (WheelView) view.findViewById(R.id.options1);
    wv_option1.setAdapter(new ArrayWheelAdapter(mOptions1Items, len));// 设置显示数据
    wv_option1.setCurrentItem(0);// 初始化时显示的数据
    // 选项2
    wv_option2 = (WheelView) view.findViewById(R.id.options2);
    if (mOptions2Items != null)
      wv_option2.setAdapter(new ArrayWheelAdapter(mOptions2Items.get(0)));// 设置显示数据
    wv_option2.setCurrentItem(wv_option1.getCurrentItem());// 初始化时显示的数据
    // 选项3
    wv_option3 = (WheelView) view.findViewById(R.id.options3);
    if (mOptions3Items != null)
      wv_option3.setAdapter(new ArrayWheelAdapter(mOptions3Items.get(0).get(0)));// 设置显示数据
    wv_option3.setCurrentItem(wv_option3.getCurrentItem());// 初始化时显示的数据


    wv_option1.setTextSize(textSize);
    wv_option2.setTextSize(textSize);
    wv_option3.setTextSize(textSize);

    if (this.mOptions2Items == null)
      wv_option2.setVisibility(View.GONE);
    if (this.mOptions3Items == null)
      wv_option3.setVisibility(View.GONE);

    // 联动监听器
    wheelListener_option1 = new OnItemSelectedListener() {

      @Override
      public void onItemSelected(int index) {
        int opt2Select = 0;
        if (mOptions2Items != null) {
          opt2Select = wv_option2.getCurrentItem();// 上一个opt2的选中位置
          // 新opt2的位置，判断如果旧位置没有超过数据范围，则沿用旧位置，否则选中最后一项
          opt2Select = opt2Select >= mOptions2Items.get(index).size() - 1 ? mOptions2Items.get(index).size() - 1
              : opt2Select;

          wv_option2.setAdapter(new ArrayWheelAdapter(mOptions2Items.get(index)));
          wv_option2.setCurrentItem(opt2Select);
        }
        if (mOptions3Items != null) {
          wheelListener_option2.onItemSelected(opt2Select);
        }
      }
    };
    wheelListener_option2 = new OnItemSelectedListener() {

      @Override
      public void onItemSelected(int index) {
        if (mOptions3Items != null) {
          int opt1Select = wv_option1.getCurrentItem();
          opt1Select = opt1Select >= mOptions3Items.size() - 1 ? mOptions3Items.size() - 1 : opt1Select;
          index = index >= mOptions2Items.get(opt1Select).size() - 1 ? mOptions2Items.get(opt1Select).size() - 1
              : index;
          int opt3 = wv_option3.getCurrentItem();// 上一个opt3的选中位置
          // 新opt3的位置，判断如果旧位置没有超过数据范围，则沿用旧位置，否则选中最后一项
          opt3 = opt3 >= mOptions3Items.get(opt1Select).get(index).size() - 1 ? mOptions3Items.get(opt1Select)
              .get(index).size() - 1 : opt3;

          wv_option3.setAdapter(new ArrayWheelAdapter(mOptions3Items.get(wv_option1.getCurrentItem()).get(index)));
          wv_option3.setCurrentItem(opt3);

        }
      }
    };

    // // 添加联动监听
    if (options2Items != null && linkage)
      wv_option1.setOnItemSelectedListener(wheelListener_option1);
    if (options3Items != null && linkage)
      wv_option2.setOnItemSelectedListener(wheelListener_option2);
  }
  
  /**
   * 设置选项的单位
   * 
   * @param label1
   * @param label2
   * @param label3
   */
  public void setLabels(String label1, String label2, String label3) {
    if (label1 != null)
      wv_option1.setLabel(label1);
    if (label2 != null)
      wv_option2.setLabel(label2);
    if (label3 != null)
      wv_option3.setLabel(label3);
  }

  /**
   * 设置是否循环滚动
   * 
   * @param cyclic
   */
  public void setCyclic(boolean cyclic) {
    wv_option1.setCyclic(cyclic);
    wv_option2.setCyclic(cyclic);
    wv_option3.setCyclic(cyclic);
  }

  /**
   * 分别设置第一二三级是否循环滚动
   * 
   * @param cyclic1
   *          ,cyclic2,cyclic3
   */
  public void setCyclic(boolean cyclic1, boolean cyclic2, boolean cyclic3) {
    wv_option1.setCyclic(cyclic1);
    wv_option2.setCyclic(cyclic2);
    wv_option3.setCyclic(cyclic3);
  }

  /**
   * 设置第二级是否循环滚动
   * 
   * @param cyclic
   */
  public void setOption2Cyclic(boolean cyclic) {
    wv_option2.setCyclic(cyclic);
  }

  /**
   * 设置第三级是否循环滚动
   * 
   * @param cyclic
   */
  public void setOption3Cyclic(boolean cyclic) {
    wv_option3.setCyclic(cyclic);
  }

  /**
   * 返回当前选中的结果对应的位置数组 因为支持三级联动效果，分三个级别索引，0，1，2
   * 
   * @return
   */
  public int[] getCurrentItems() {
    int[] currentItems = new int[3];
    currentItems[0] = wv_option1.getCurrentItem();
    currentItems[1] = wv_option2.getCurrentItem();
    currentItems[2] = wv_option3.getCurrentItem();
    return currentItems;
  }

  public void setCurrentItems(int option1, int option2, int option3) {
    if (linkage && wv_option1.getCurrentItem() != option1) {
      wheelListener_option1.onItemSelected(option1);
    } else if (linkage && wv_option2.getCurrentItem() != option2) {
      wheelListener_option2.onItemSelected(option2);
    }
    wv_option1.setCurrentItem(option1);
    wv_option2.setCurrentItem(option2);
    wv_option3.setCurrentItem(option3);
  }
}
